package dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import domain.FanUsers;
import domain.Item;
import domain.ItemImg;
import domain.KeyWord;
import domain.LookedUsers;
import domain.User;

public class ItemDaoImpl implements ItemDao {
	private SessionFactory sessionFactory;
	private FansDao fansDao;
	private AdminDao adminDao;
	
	public FansDao getFansDao() {
		return fansDao;
	}

	public void setFansDao(FansDao fansDao) {
		this.fansDao = fansDao;
	}
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	/*
	 * (non-Javadoc)
	 * @see dao.itemDao#getAllitem()
	 */
	@Override
	public List getAllItem(String sellOrBuy) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
//		Item tempItem = (Item) listIt.next();
		List list = new ArrayList();	
		if(sellOrBuy.equals("shop")){
				 List allList = session.createQuery("from Item where isdel = ?").setString(0, "1").list();
				 Iterator it = allList.iterator();
				 while(it.hasNext()){
					 Item itemTemp = (Item) it.next();
					 if(itemTemp.getOwner().getShopOrPerson()!=null){
					 if(itemTemp.getOwner().getShopOrPerson().equals("店铺")){
						 list.add(itemTemp);
					 }
					 }
				 }
				 
			}else if(sellOrBuy.equals("free")){
				list = session.createQuery("from Item where isdel = ? and price = ?").setString(0, "1").setDouble(1, 0).list();
			}
			else if(sellOrBuy.equals("sell")|sellOrBuy.equals("buy")){
				list = session.createQuery("from Item where isdel = ? and sellOrbuy = ?").setString(0, "1").setString(1, sellOrBuy).list();
		}
		if(list.size()>=1){
		Item i = (Item)list.get(0);
		}
//		ItemImg ii = i.getIi();
		//HibernateSessionFactory.closeSession();
		return list;
}
	
	
	
	@Override
	public List getPersonItem(String sellOrBuy,int userId) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
//		Item tempItem = (Item) listIt.next();
		List list = new ArrayList();	
				list = session.createQuery("from Item where isdel = ? and sellOrbuy = ? and owner_id=?").setString(0, "1").setString(1, sellOrBuy).setInteger(2, userId).list();
		if(list.size()>=1){
		Item i = (Item)list.get(0);
		}
//		ItemImg ii = i.getIi();
		//HibernateSessionFactory.closeSession();
		return list;
}
	@Override
	public void insertKeyWord(String keyWord){
		Session session=sessionFactory.getCurrentSession();
		Query q = session.createQuery("from KeyWord where name = ?");
		q.setString(0,keyWord);
		List list = q.list();
		if(list.size()>=1){
			KeyWord keyWordObject = (KeyWord) list.get(0);
			keyWordObject.setNum(keyWordObject.getNum()+1);
			session.saveOrUpdate(keyWordObject);
		}else{
			KeyWord keyWordObject = new KeyWord();
			keyWordObject.setName(keyWord);
			keyWordObject.setNum(1);
			session.save(keyWordObject);
		}
	}
	@Override
	public List getFanedItem(String acc,String sellOrBuy) {
		// TODO Auto-generated method stub
		
		Session session=sessionFactory.getCurrentSession();
		Query q = session.createQuery("from User where ACCOUNT = ?");
		q.setString(0, acc);
		List <User>users = q.list();
		User user = null;
		if(users.size()>0){
			user = users.get(0);
		}
		List allList = new ArrayList();
		List faned = fansDao.showFaned(acc);
		Iterator<FanUsers> it = faned.iterator();
		List list = new ArrayList();
		while(it.hasNext()){
			list =it.next().getFanedUser().getSellItem();
			Iterator listIt = list.iterator();
			while(listIt.hasNext()){
				Item tempItem = (Item) listIt.next();
				if(tempItem.getIsDel().equals("1")){
					if(sellOrBuy.equals("shop")){
						if(tempItem.getOwner().getShopOrPerson()!=null){
						if(tempItem.getOwner().getShopOrPerson().equals("店铺")){
							allList.add(tempItem);
						}
					}
					}else if(sellOrBuy.equals("free")){
						if(tempItem.getPrice()==0){
							allList.add(tempItem);
						}
					}
					else if(sellOrBuy.equals("sell")|sellOrBuy.equals("buy")){
					if(tempItem.getSellOrBuy().equals(sellOrBuy)){
						allList.add(tempItem);
					}
					}
				}
			}
		}
		
		if(allList.size()>=1){
		  Item temp = (Item)allList.get(0);
		  temp.getClassified();
		}
		return allList;
	}
	
	
	@Override
	public List getPersonFanedItem(int userId,String sellOrBuy) {
		// TODO Auto-generated method stub
		
		Session session=sessionFactory.getCurrentSession();
		User user= (User)session.get(User.class, userId);
		List allList = new ArrayList();
		List faned = fansDao.showFaned(user.getAccount());
		Iterator<FanUsers> it = faned.iterator();
		List list = new ArrayList();
		while(it.hasNext()){
			list =it.next().getFanedUser().getSellItem();
			Iterator listIt = list.iterator();
			while(listIt.hasNext()){
				Item tempItem = (Item) listIt.next();
				if(tempItem.getIsDel().equals("1")){
					
					
						allList.add(tempItem);
					
					
				}
			}
		}
		
		if(allList.size()>=1){
		  Item temp = (Item)allList.get(0);
		  temp.getClassified();
		}
		return allList;
	}
	
	@Override
	public List getFansItem(String acc,String sellOrBuy) {
		// TODO Auto-generated method stub
		
		Session session=sessionFactory.getCurrentSession();
		Query q = session.createQuery("from User where ACCOUNT = ? ");
		q.setString(0, acc);
		List <User>users = q.list();
		User user = null;
		if(users.size()>0){
			user = users.get(0);
		}
		List allList = new ArrayList();
		List fans = fansDao.showFans(acc);
		Iterator<FanUsers> it = fans.iterator();
		List list = new ArrayList();
		while(it.hasNext()){
			list =it.next().getFanUser().getSellItem();
			Iterator listIt = list.iterator();
			while(listIt.hasNext()){
				Item tempItem = (Item) listIt.next();
				if(tempItem.getIsDel().equals("1")){
					if(sellOrBuy.equals("shop")){
						if(tempItem.getOwner().getShopOrPerson()!=null){
						if(tempItem.getOwner().getShopOrPerson().equals("店铺")){
							allList.add(tempItem);
						}
						}
					}else if(sellOrBuy.equals("free")){
						if(tempItem.getPrice()==0){
							allList.add(tempItem);
						}
					}
					else if(sellOrBuy.equals("sell")|sellOrBuy.equals("buy")){
					if(tempItem.getSellOrBuy().equals(sellOrBuy)){
						allList.add(tempItem);
					}
					}
				}
			}
		}
		
		if(allList.size()>=1){
		  Item temp = (Item)allList.get(0);
		  temp.getClassified();
		}
		return allList;
	}
	
	@Override
	public List getPersonFansItem(int userId,String sellOrBuy) {
		// TODO Auto-generated method stub
		
		Session session=sessionFactory.getCurrentSession();
		User user= (User)session.get(User.class, userId);
		List allList = new ArrayList();
		List fans = fansDao.showFans(user.getAccount());
		Iterator<FanUsers> it = fans.iterator();
		List list = new ArrayList();
		while(it.hasNext()){
			list =it.next().getFanUser().getSellItem();
			Iterator listIt = list.iterator();
			while(listIt.hasNext()){
				Item tempItem = (Item) listIt.next();
				if(tempItem.getIsDel().equals("1")){
						allList.add(tempItem);
				}
			}
		}
		
		if(allList.size()>=1){
		  Item temp = (Item)allList.get(0);
		  temp.getClassified();
		}
		return allList;
	}
	/*插入查看物品人的信息
	 * (non-Javadoc)
	 * @see dao.ItemDao#insertItemLookUser(java.lang.String, java.lang.String)
	 */
	@Override
	public Item insertItemLookUser(String acc, String item_id){
		Session session=sessionFactory.getCurrentSession();
		int i = 0;
		if(item_id != null){
			 i = Integer.parseInt(item_id);
		}
		
		Query q = session.createQuery("from Item where item_id = ?");
		q.setInteger(0, i);
		List <Item>list = q.list();
		Item item = null;
		if(list.size()>0){
			item = list.get(0);
		}
//		List<User> userList = item.getUser_look();
		q = session.createQuery("from User where ACCOUNT = ?");
		q.setString(0, acc);
		List <User>users = q.list();
		User user = null;
		if(users.size()>0){
			user = users.get(0);
		}
		LookedUsers lu = new LookedUsers();
//		session=sessionFactory.getCurrentSession();
		q = session.createQuery("from LookedUsers where ITEM_ID = ? and USER_ID =?");
		q.setInteger(0, item.getItem_id());
		q.setInteger(1, user.getId());
		List judge = q.list();
//		System.out.println(judge.size());
		if (judge.size() == 0) {
			lu.setU(user);
			lu.setLook_time(new Date());
			lu.setItem(item);
			session.saveOrUpdate(lu);
			item.getUser_look().add(lu);
			// item.getUser_look().add(user);
			session.saveOrUpdate(item);
		} else {
			item.getUser_look();
		}
		return item;
	}
	
//	@Override
//	public User getItemLookUsersByItem_id(String item_id){
//		Session session=sessionFactory.getCurrentSession();
//		int i = 0;
//		if(item_id != null){
//			 i = Integer.parseInt(item_id);
//		}
//		
//		Query q = session.createQuery("from Item where item_id = ?");
//		q.setInteger(0, i);
//		List <Item>list = q.list();
//		Item item = null;
//		List<User>  users = null;
//		if(list.size()>0){
//			item = list.get(0);
//			users = item.getUser_look();
//		}
//		Iterator it = users.iterator();
//		q = session.createQuery("from User where ACCOUNT = ?");
//		q.setString(0, acc);
//		List <User>users = q.list();
//		User user = null;
//		if(users.size()>0){
//			user = users.get(0);
//		}
//		item.getUser_look().add(user);
//		return null;
//	}
	/*
	 * //Ϊʲ插入卖的物品
	 * (non-Javadoc)
	 * @see dao.itemDao#insertitem(domain.item)
	 */
	@Override
	public void insertSellItem(Item item,String path){
		ItemImg ii = new ItemImg();
		ii.setPath(path);
		item.setIi(ii);
	
		
		Session session=sessionFactory.getCurrentSession();
		session.saveOrUpdate(item);
	}

	@Override
	public List<Item> queryItem(String q, String sellOrBuy) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Item  where name like ? and isdel = ? and sellOrBuy = ?");
		query.setString(0, "%" + q + "%");
		query.setString(1, "1");
		query.setString(2, sellOrBuy);
		List list = query.list();
		insertKeyWord(q);
		return list;
	}

	@Override
	public List showOwnerItem(String acc,String sellOrBuy ) {
		// TODO Auto-generated method stub
		User user = adminDao.getUserByAcc(acc);
		int userId = user.getId();
		Session session=sessionFactory.getCurrentSession();
		Query q = session.createQuery("from Item where OWNER_ID = ? and SELLORBUY =? and isdel = ?");
		q.setInteger(0, userId);
		q.setString(1, sellOrBuy);
		q.setString(2, "1");
		List list = q.list();
		return list;
	}

	public AdminDao getAdminDao() {
		return adminDao;
	}

	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}

	@Override
	public Item updateItem(String item_id, String ownSellOrBuy) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		int itemId = 0;
		if(item_id!=null){
			itemId = Integer.parseInt(item_id);
		}
		Item item = (Item) session.get(Item.class,itemId );
		return item;
	}

	@Override
	public int updateItemOperate(Item item) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		
		Item dbItem = (Item)session.get(Item.class, item.getItem_id());
		dbItem.setName(item.getName());
		dbItem.setNum(item.getNum());
		dbItem.setOld_new(item.getOld_new());
		dbItem.setPrice(item.getPrice());
		dbItem.setClassified(item.getClassified());
		dbItem.setColor(item.getColor());
		dbItem.setState(item.getState());
		dbItem.setName(item.getName());
		int i = 0;
		session.saveOrUpdate(dbItem);
		i = 1;
		return i;
	}

	@Override
	public void deleteItem(String item_id) {
		// TODO Auto-generated method stub
		int itemId = 0;
		Session session=sessionFactory.getCurrentSession();
		if(item_id!=null){
			itemId = Integer.parseInt(item_id);
		}
		Item item = (Item) session.get(Item.class,itemId);
		item.setIsDel("0");
		session.saveOrUpdate(item);
	}

	@Override
	public List showKeyWord() {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		Query q = session.createQuery("from KeyWord order by num desc");
		q.setMaxResults(3);
		List list = q.list();
		return list;
	}
}
